# 1.登录进入litemall
# 2.点击商场管理
# 3.点击品牌制造商
# 4.点击添加按钮
# 5.输入需要添加的信息
# 6.点击确定按钮
# 7.验证添加成功（弹窗和列表中有新增的数据）


from selenium import webdriver
from  selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
from utils import log
#自定义显示等待
def click_exception(by,element,attempts=5):
       def _inner(driver):
              #实际循环次数
              count=0
              while count<attempts:
                    try:
                        count+=1
                        driver.find_element(by,element).click()
                    except Exception:
                          print("出现异常了")
              return False
       return _inner
                     
def test_login():
        driver=webdriver.Chrome()
        # 添加隐式等待配置
        driver.implicitly_wait(3)
        driver.get("http://litemall.hogwarts.ceshiren.com/#/dashboard")
        #输入用户名和密码
        driver.find_element(By.NAME,"username").clear()
        driver.find_element(By.NAME,"username").send_keys("hogwarts")
        driver.find_element(By.NAME,"password").clear()
        driver.find_element(By.NAME,"password").send_keys("test12345")
        #点击登录按钮
        driver.find_element(By.CLASS_NAME,"el-button").click()
        driver.maximize_window()
        # 点击
        driver.find_element(By.XPATH,"//*[text()='商场管理']").click()
        #点击
        driver.find_element(By.XPATH,"//*[text()='通用问题']").click()
        driver.find_element(By.XPATH,"//*[text()='添加']").click()

        #填充表单内容
        driver.find_element(By.XPATH,"//label[text()='问题']/following-sibling::div//input").send_keys("1111")
    
        driver.find_element(By.XPATH,"//label[text()='回复']/following-sibling::div//textarea").send_keys("11111222223333")

        # time.sleep(3)
        WebDriverWait(driver,10).until(click_exception(By.CSS_SELECTOR,".dialog-footer .el-button--primary"))
        #点击确定按钮
        driver.find_element(By.CSS_SELECTOR,".dialog-footer .el-button--primary").click()

        #alert弹窗校验
        find_ele=driver.find_element(By.XPATH,"//*[text()='创建成功']")
        print(find_ele.text)
        #assert "创建成功" in find_ele.text

        #列表内容校验
        # time.sleep(5)
        WebDriverWait(driver,10).until(click_exception(By.XPATH,"//*[text()='1111']"))
        ele=driver.find_element(By.XPATH,"//*[text()='1111']")
        
        driver.find_element(By.XPATH,"//*[text()='1111']/../..//*[text()='删除']").click()
        log.logger.info(f"断言到的实际结果为：{ele}")
        assert ele.text!=[]

        driver.quit()


def delete_ele():
        driver=webdriver.Chrome()
        # 添加隐式等待配置
        driver.implicitly_wait(3)
        driver.get("http://litemall.hogwarts.ceshiren.com/#/dashboard")
        #输入用户名和密码
        driver.find_element(By.NAME,"username").clear()
        driver.find_element(By.NAME,"username").send_keys("hogwarts")
        driver.find_element(By.NAME,"password").clear()
        driver.find_element(By.NAME,"password").send_keys("test12345")
        #点击登录按钮
        driver.find_element(By.CLASS_NAME,"el-button").click()
        driver.maximize_window()
        # 点击
        driver.find_element(By.XPATH,"//*[text()='商场管理']").click()
        #点击
        driver.find_element(By.XPATH,"//*[text()='通用问题']").click()
        driver.find_element(By.XPATH,"//*[text()='添加']").click()

        #填充表单内容
        driver.find_element(By.XPATH,"//label[text()='问题']/following-sibling::div//input").send_keys("删除1111")
    
        driver.find_element(By.XPATH,"//label[text()='回复']/following-sibling::div//textarea").send_keys("11111222223333")
        #点击确定按钮
        driver.find_element(By.CSS_SELECTOR,".dialog-footer .el-button--primary").click()
        

        # time.sleep(3)
        WebDriverWait(driver,10).until(click_exception(By.XPATH,"//*[text()='删除1111']/../..//*[text()='删除']"))

        driver.find_element(By.XPATH,"//*[text()='删除1111']/../..//*[text()='删除']").click()

        #alert弹窗校验
        find_ele=driver.find_element(By.XPATH,"//*[text()='删除成功']")
        print(find_ele.text)
        #assert "创建成功" in find_ele.text

        #列表内容校验
        # time.sleep(5)
        WebDriverWait(driver,10).until(click_exception(By.XPATH,"//*[text()='删除1111']"))
        ele=driver.find_element(By.XPATH,"//*[text()='删除1111']")
        log.logger.info(f"断言到的实际结果为：{ele}")
        assert ele.text==[]

        driver.quit()


if __name__ == '__main__':
    test_login()
    delete_ele()